package com.yangxiangrice.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangxiangrice.pojo.*;
import com.yangxiangrice.service.GrowSeedlingsService;
import com.yangxiangrice.service.ProdPlanService;
import com.yangxiangrice.service.SeedHoldService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @author: shixiaojian
 * @date: 2021年11月23日 9:31
 */
@Controller
@RequestMapping("/seedhold")
@CrossOrigin
@Slf4j
public class SeedHoldController {

    @Autowired
    SeedHoldService seedHoldService;
    @Autowired
    ProdPlanService prodPlanService;
    @Autowired
    GrowSeedlingsService growSeedlingsService;


    @GetMapping(value = "/getplan")
    @ResponseBody
    public Result getseed() {
        Result<List> result = new Result<>();
        Option option = null;
        List<Option> res = new ArrayList<>();
        List<ProdPlan> list = prodPlanService.list();
        for (ProdPlan prodPlan : list) {
            option = new Option();
            option.setLabel(prodPlan.getPlanCode());
            option.setValue(prodPlan.getPlanCode());
            res.add(option);
        }
        return result.ok(res);
    }

    @GetMapping(value = "/listpage")
    @ResponseBody
    public Result listpage(@RequestParam("page") int page,
                           @RequestParam("limit") int limit) {
        Result<IPage> result = new Result<>();
        Subject subject = SecurityUtils.getSubject();
        String phone = subject.getPrincipal().toString();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("phone", phone);
        IPage<SeedHold> seedHoldIPage = seedHoldService.seletePage(new Page<>(page, limit), wrapper);
        return result.ok(seedHoldIPage);
    }

    @PostMapping(value = "/save")
    @ResponseBody
    public Result save(@RequestBody SeedHold seedHold) {
        Result<Boolean> result = new Result<>();
        seedHold.setCreatetime(new Timestamp(System.currentTimeMillis()));
        if (ObjectUtil.isEmpty(seedHold.getSeedHoldId())) {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("planCode", seedHold.getPlanCode());
            ProdPlan prodPlan = prodPlanService.getOne(wrapper);
            Subject subject = SecurityUtils.getSubject();
            String phone = subject.getPrincipal().toString();
            seedHold.setPlanId(prodPlan.getPlanId());
            seedHold.setSeedName(prodPlan.getSeedName());
            seedHold.setPhone(phone);
            boolean tag = seedHoldService.save(seedHold);
            if (tag) {
                result.ok(tag);
            } else {
                result.fail("添加失败");
            }
        }else{
            boolean tag = seedHoldService.updateById(seedHold);
            if (tag) {
                result.ok(tag);
            } else {
                result.fail("更新失败");
            }
        }
        return result;
    }

    @PostMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestParam(value = "seedHoldId") Integer seedHoldId) {
        Result<Boolean> result = new Result<>();
        boolean tag = seedHoldService.removeById(seedHoldId);
        if (tag) {
            result.ok(tag);
        } else {
            result.fail("删除失败");
        }
        return result;
    }

    @PostMapping(value = "/deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestBody int[] ids) {
        ArrayList<Integer> list = new ArrayList<>();
        Result<Boolean> result = new Result<>();
        for (int id : ids) {
            list.add(id);
        }
        boolean tag = seedHoldService.removeByIds(list);
        if (tag) {
            result.ok(tag);
        } else {
            result.fail("删除失败");
        }
        return result;
    }

    @RequestMapping(value = "/search")
    @ResponseBody
    public Result search(@RequestParam("input") String input,
                         @RequestParam("page") int page,
                         @RequestParam("limit") int limit) {
        Result<IPage> result = new Result<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("seedName", input);
        IPage<SeedHold> seedHoldIPage = seedHoldService.seletePage(new Page<>(page, limit), wrapper);
        if (ObjectUtil.isEmpty(seedHoldIPage)) {
            result.fail("未找到符合条件的数据");
        } else {
            result.ok(seedHoldIPage);
        }
        return result;
    }

    @GetMapping(value = "/getseeddata")
    @ResponseBody
    public Result getseeddata() {
        Subject subject = SecurityUtils.getSubject();
        String phone = subject.getPrincipal().toString();
        Result<List> result = new Result();
        ArrayList<EchartData> res = new ArrayList<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("DISTINCT seedName");
        wrapper.eq("phone", phone);
        List<SeedHold> list = seedHoldService.list(wrapper);
        EchartData data = null;
        for (SeedHold o : list) {
            data = new EchartData();
            String seedName = o.getSeedName();
            data.setName(seedName);
            Integer purchase = seedHoldService.getPurchaseNum(seedName);
            if (!ObjectUtil.isEmpty(purchase)) {
                data.setPurchase(purchase);
            }
            Integer consume = growSeedlingsService.getConsumeNum(seedName);
            if (!ObjectUtil.isEmpty(consume)) {
                data.setConsume(consume);
            }
            res.add(data);
        }
        result.ok(res);
        return result;
    }

    @GetMapping(value = "/getadminseeddata")
    @ResponseBody
    public Result getadminseeddata() {
        Result<List> result = new Result();
        ArrayList<EchartData> res = new ArrayList<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("DISTINCT seedName");
        List<SeedHold> list = seedHoldService.list(wrapper);
        EchartData data = null;
        for (SeedHold o : list) {
            data = new EchartData();
            String seedName = o.getSeedName();
            data.setName(seedName);
            Integer purchase = seedHoldService.getPurchaseNum(seedName);
            if (!ObjectUtil.isEmpty(purchase)) {
                data.setPurchase(purchase);
            }
            Integer consume = growSeedlingsService.getConsumeNum(seedName);
            if (!ObjectUtil.isEmpty(consume)) {
                data.setConsume(consume);
            }
            res.add(data);
        }
        result.ok(res);
        return result;
    }
}
